unit CommonScripts;

interface

uses
  Classes, Controls, Forms, DB, ExtCtrls, Menus, Grids,
  DBGrids, PortalGrid, StdCtrls, Buttons,
  SynEditHighlighter, SynHighlighterDWS, SynEdit, SynDBEdit;

type
  TFormUnits = class(TForm)
    ImagePointer: TImage;
    Label2: TLabel;
    BtnClear: TBitBtn;
    BtnDeleteScript: TBitBtn;
    PanelNewUnit: TPanel;
    Label1: TLabel;
    BtnUpdate: TBitBtn;
    BtnAddUnit: TBitBtn;
    EditUnit: TEdit;
    Panel1: TPanel;
    Status: TLabel;
    BtnHelp: TBitBtn;
    BtnDone: TBitBtn;
    Script: TDBSynEdit;
    EditCopy: TEdit;
    GridUnits: TPortalGrid;
    MainMenu1: TMainMenu;
    Main1: TMenuItem;
    MenuHelp: TMenuItem;
    CtrlF1: TMenuItem;
    CtrlR1: TMenuItem;
    CtrlZ1: TMenuItem;
    Esc1: TMenuItem;
    CtrlA1: TMenuItem;
    TimerPointer: TTimer;
    DSUnits: TDataSource;
    SynDWSSyn1: TSynDWSSyn;
    Bevel1: TBevel;
    procedure BtnDoneClick(Sender: TObject);
    procedure DSUnitsDataChange(Sender: TObject; Field: TField);
    procedure BtnClearClick(Sender: TObject);
    procedure GridUnitsDblClick(Sender: TObject);
    procedure EditUnitKeyPress(Sender: TObject; var Key: Char);
    procedure BtnAddUnitClick(Sender: TObject);
    procedure BtnDeleteUnitClick(Sender: TObject);
    procedure GridUnitsKeyPress(Sender: TObject; var Key: Char);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure BtnHelpClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure TimerPointerTimer(Sender: TObject);
  private
    { Private declarations }
    procedure UpdatePointer;
  public
    { Public declarations }
  end;

var
  FormUnits: TFormUnits;

implementation
uses
  Windows, SysUtils, Dialogs, DBClient, Variants,
  Database, Functions, VarGrabber, AM, refactorings, ScriptModule;

{$R *.DFM}

procedure TFormUnits.BtnDoneClick(Sender: TObject);
begin
  close;
end;

procedure TFormUnits.DSUnitsDataChange(Sender: TObject; Field: TField);
begin
  TimerPointer.enabled:=true;
end;

procedure TFormUnits.BtnClearClick(Sender: TObject);
begin
  if Script.lines.count < 1 then exit;
  Script.lines.clear;
  Script.setfocus;
end;

procedure TFormUnits.GridUnitsDblClick(Sender: TObject);
begin
  Script.setfocus;
end;

procedure TFormUnits.EditUnitKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    BtnAddUnit.click;
    key:=#0;
  end;
end;

procedure TFormUnits.BtnAddUnitClick(Sender: TObject);
var
  didfind, replacing: boolean;
begin
  replacing:=false;
  if TBitBtn(sender).tag = 1 then replacing:=true;
  if (replacing) and (Datawell.TableUnits.fieldbyname('Name').asstring = '') then begin
    messagedlg('There is no unit to replace',mtinformation,[mbok],0);
    EditUnit.setfocus;
    exit;
  end;
  if EditUnit.text = '' then begin
    messagedlg('You must enter a unit name',mtinformation,[mbok],0);
    EditUnit.setfocus;
    exit;
  end;
  if not Datawell.ValidIdentifier(EditUnit.text) then begin
    messagedlg('Name must be a valid identifier (first character must be a letter, ' +
      'and every character after must be a letter, number or underscore',
      mtinformation,[mbok],0);
    EditUnit.selectall;
    EditUnit.setfocus;
    exit;
  end;
  EditUnit.text:=Datawell.CutTrailingSpaces(EditUnit.text);
  if not replacing then begin
    Datawell.TableUnits.append;
    Datawell.TableUnits.fieldbyname('Master').asinteger:=Datawell.masterinteger;
    Datawell.TableUnits.fieldbyname('Name').asstring:=EditUnit.text;
    Datawell.TableUnits.post;
    Script.lines.clear;
  end
  else begin
    Datawell.TableUnits.edit;
    Datawell.TableUnits.fieldbyname('Name').asstring:=EditUnit.text;
    Datawell.TableUnits.post;
  end;
  EditUnit.text:='';
  Script.setfocus;
end;

procedure TFormUnits.BtnDeleteUnitClick(Sender: TObject);
begin
  if not Datawell.TableUnits.EOF then Datawell.TableUnits.delete;
  TimerPointer.enabled:=true;
end;

procedure TFormUnits.GridUnitsKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    GridUnitsDblClick(Sender);
  end;
end;

procedure TFormUnits.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormUnits.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('units');
end;

procedure TFormUnits.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key = VK_DELETE) and (activecontrol = GridUnits) then begin
    BtnDeleteScript.click;
    key:=0;
  end;
end;

procedure TFormUnits.UpdatePointer;
begin
  DrawArrow(GridUnits, ImagePointer, 297, 6, 5);
end;

procedure TFormUnits.TimerPointerTimer(Sender: TObject);
begin
  UpdatePointer;
  TimerPointer.enabled:=false;
end;

end.
